/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FindUser.java
 *
 * Created on Jun 26, 2011, 8:36:26 PM
 */
package chat;

import chat.server.model.User;
import javax.swing.DefaultListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author mingq
 */
public class FindUser extends javax.swing.JDialog {

    private String mSelectedUid = null;
    private String mMyUid;
    private ChatMain mParent;
    private DefaultListModel users;

    /** Creates new form FindUser */
    public FindUser(java.awt.Frame parent, boolean modal, ChatMain pa, String strFriends) {
        super(parent, modal);
        initComponents();
        mParent = pa;
//        mParent

        mParent.mServerClient.getAllUserNotFriend(mParent.getLoginedUid());


    }

    public void setUsers(String strFriends) {
        users = new DefaultListModel();
        String[] nicks = strFriends.split("\t");
        for (int i = 0; i < nicks.length; i++) {
            String[] splits = nicks[i].split("#");
            User user = new User(splits[1], splits[0], "");
            user.online = Boolean.valueOf(splits[2]);
            users.addElement(user);
        }

        lstUser.setCellRenderer(new ListItemUser());
        lstUser.setModel(users);
        users.addListDataListener(new ListDataListener() {

            public void intervalAdded(ListDataEvent e) {
                System.out.println("contentsadd on:");
            }

            public void intervalRemoved(ListDataEvent e) {
                System.out.println("contentsRevmoe on:");
            }

            public void contentsChanged(ListDataEvent e) {
                User o = (User) ((DefaultListModel) e.getSource()).getElementAt(e.getIndex0());
                System.out.println("contentsChanged on: " + o.toString());
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstUser = new javax.swing.JList();
        添加好友 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lstUser.setModel(new javax.swing.AbstractListModel() {
            String[] strings = {  };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstUser.setName("lstUser"); // NOI18N
        jScrollPane1.setViewportView(lstUser);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(chat.ChatApp.class).getContext().getResourceMap(FindUser.class);
        添加好友.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        添加好友.setName("jLabel1"); // NOI18N

        btnAdd.setLabel(resourceMap.getString("btnAdd.label")); // NOI18N
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });

        btnCancel.setLabel(resourceMap.getString("btnCancel.label")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(添加好友)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 256, Short.MAX_VALUE)
                        .addComponent(btnCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(添加好友)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnCancel))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
        System.out.println(lstUser.getSelectedValue().toString());
        User user = (User) lstUser.getSelectedValue();
        String uid = user.uid;
        mMyUid = mParent.getLoginedUid();
        mParent.setAddUser(user);
        mParent.mServerClient.addFriend(mMyUid, uid);
    }//GEN-LAST:event_btnAddMouseClicked

    private void btnCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelMouseClicked
        dispose();
    }//GEN-LAST:event_btnCancelMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstUser;
    private javax.swing.JLabel 添加好友;
    // End of variables declaration//GEN-END:variables
}
